/**
 *
 * Copyright 2009 garrett
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License.  You may obtain a copy of the
 * License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License. 
 */
package org.memesis.pandorasbook;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.memesis.pandorasbook.util.ActiveMQHelper;

/**
 * A facade over the JMS system, allowing applets easy access to
 * message queue functionality.
 * 
 * @author garrett
 */
public class PandoraMessageQueue {
	private Connection conn = null;
	private Session session = null;
	private Map<String, MessageProducer> producerMap;
	private Map<String, MessageConsumer> consumerMap;
	
	public PandoraMessageQueue(PandoraApplet applet) throws JMSException {
		producerMap = new HashMap<String, MessageProducer>();
		consumerMap = new HashMap<String, MessageConsumer>();
		
		// Look through the applet for appropriate properties
		String mqUrl = applet.getProperty("jms-url");
		if(mqUrl != null) {
			conn = ActiveMQHelper.getConnection(mqUrl);
			session = ActiveMQHelper.createSession(conn);
		}
	}
	
	public void terminate() {
		try {
			if(null != session)
				session.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public void publish(String queue, String message) throws JMSException {
		if(null != session)
			ActiveMQHelper.SendTextMessage(session, getProducer(queue), message);
		else
			throw new JMSException("JMS is not enabled for this applet");
	}
	
	public void subscribe(String queue, MessageListener listener) throws JMSException {
		if(null != session)
			getConsumer(queue).setMessageListener(listener);
		else
			throw new JMSException("JMS is not enabled for this applet");
	}
	
	private MessageProducer getProducer(String topic) throws JMSException {
		if(!producerMap.containsKey(topic)) {
			Destination dest = ActiveMQHelper.createDestination(session, topic, true);
			MessageProducer producer = ActiveMQHelper.createMessageProducer(session, dest);			
			producerMap.put(topic, producer);
		}
		return producerMap.get(topic);
	}
	
	private MessageConsumer getConsumer(String topic) throws JMSException {
		if(!consumerMap.containsKey(topic)) {
			Destination dest = ActiveMQHelper.createDestination(session, topic, true);
			MessageConsumer consumer = ActiveMQHelper.createMessageConsumer(session, dest);
			consumerMap.put(topic, consumer);
		}
		return consumerMap.get(topic);
	}
}